﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Drawing;
using System.Drawing.Drawing2D;

/// <summary>
/// Summary description for clsImages
/// </summary>
public class clsImages
{
    public clsImages()
    {
    
    }  
    public void saveAndResizeImage(string fileName, string newFileName ,int newWith , int newHieght , string  pmr )
    {
        Bitmap bm = new Bitmap(fileName);
        Bitmap thumb = new Bitmap(newWith, newHieght);

        Graphics g= Graphics.FromImage(thumb);
        g.InterpolationMode = InterpolationMode.HighQualityBicubic;

        int x1;
        int x2;
        int y1;
        int y2;

        Single tl;

        if (bm.Width / newWith > bm.Height / newHieght)
            tl = bm.Height / newHieght;
        else
            tl = bm.Width / newWith;

        if (tl < 1)
        {
            y1 = 0;
            y2 = bm.Height;
            x1 = 0;
            x2 = bm.Width;
        }
        else{
            y1 = Convert.ToInt32( (bm.Height - newHieght * tl) / 2);
            y2 = bm.Height - y1;
            x1 = Convert.ToInt32((bm.Width - newWith * tl) / 2);
            x2 = bm.Width - x1;
        }

        g.DrawImage(bm, new Rectangle(0, 0, newWith, newHieght), new Rectangle(x1, y1, x2, y2), GraphicsUnit.Pixel);

        g.Dispose();
        bm.Dispose();

        switch  (pmr.ToLower())
        {
            case  ".gif":
                thumb.Save(newFileName, System.Drawing.Imaging.ImageFormat.Gif);
                break;
            case ".png":
                thumb.Save(newFileName, System.Drawing.Imaging.ImageFormat.Png);
                break;
            case ".jpg":
                thumb.Save(newFileName, System.Drawing.Imaging.ImageFormat.Jpeg);
                break;
        }
        thumb.Dispose();

    }
}
